package com.xjj.datarources.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjj.datarources.common.AppCode;
import com.xjj.datarources.dao.MysqlMapper;
import com.xjj.datarources.dao.SQLMapper;
import com.xjj.datarources.exception.APIException;
import com.xjj.datarources.service.MysqlVoService;
import com.xjj.datarources.vo.MysqlVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * @name: xjj
 * @date: 2021/5/20 14:34
 */
@Service
public class MysqlVoImpl  extends ServiceImpl<MysqlMapper, MysqlVo> implements MysqlVoService {


    @Resource
    private SQLMapper mapper;

    @Resource
    private MysqlMapper mysqlMapper;

    @Override
    //@Transactional
    public IPage<MysqlVo> getMysqlPage() {

        Page<MysqlVo> page = new Page<>(1,2);

        QueryWrapper<MysqlVo> queryWrapper = new QueryWrapper<>();


        try {
            IPage<MysqlVo> iPage = mysqlMapper.selectAdminByPage(page, queryWrapper);

            return iPage;
        }catch (Exception e){
            throw new APIException("出现某个问题了");
        }
//
        //return
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public String updateNameById(String id) {


        UpdateWrapper<MysqlVo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("name", "sb").eq("id",id);

        try {
            mysqlMapper.update(null, updateWrapper);
            //mysqlMapper.updateById(id);
            int a= 10/ 0;
            return id;
        } catch (Exception e) {
            log.error(e.getMessage());
        //AppCode.NAME_NOT_EXIST,
            throw new APIException("不存在的name, id="+id);
        }
    }
}
